package br.com.webcrm.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.webcrm.dao.UsuarioDAO;
import br.com.webcrm.model.Usuario;

@Repository(value = "usuarioDao")
public class UsuarioDAOImpl extends BaseDAOImpl<Usuario, Integer> implements  UsuarioDAO {


	@Autowired
	public UsuarioDAOImpl(@Qualifier("sessionFactory") SessionFactory factory) {
		super.setSessionFactory(factory);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public List<Usuario> consultarTodos() {

		return consultarPorNamedQuery("Usuario.findAll");

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Usuario logar(String login, String senha) {
		String query = "SELECT u FROM Usuario u WHERE u.login = :login AND u.senha = :senha";
		String[] nomes = {"login", "senha"};
		Object[] valores = {login, senha};
		@SuppressWarnings("unchecked")
		List<Usuario> lista = getHibernateTemplate().findByNamedParam(query, nomes, valores);
		if (lista.size() <= 0)
			return null;
		else
			return lista.get(0);
	}

	@Override
	public List<Usuario> consultarPorIdGrupo(Integer IdGrupo) {
		String[] nomes = { "idGrupo", "ativo" };
		Object[] valores = { IdGrupo, new Boolean(true) };
		return this.consultarPorNamedQueryEParametros("Usuario.findByIdGrupo", nomes, valores);
		
	}
	
}
